package com.ly.emp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.entity.HubTeamAddEvaluate;
import com.ly.emp.mapper.HubTeamAddEvaluateMapper;
import com.ly.emp.service.IHubTeamAddEvaluateService;
import com.ly.common.core.bean.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * 一线班组-季度/年度加减分评价-impl
 *
 * @author luoyun
 * @Date: 2025-06-20
 */
@Service
public class HubTeamAddEvaluateServiceImpl extends ServiceImpl<HubTeamAddEvaluateMapper, HubTeamAddEvaluate> implements IHubTeamAddEvaluateService {

    @Override
    public R getId(String id) {
        HubTeamAddEvaluate entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamAddEvaluate entity) {
        List<HubTeamAddEvaluate> list = this.list(
                new LambdaQueryWrapper<HubTeamAddEvaluate>()
                        .orderByDesc(HubTeamAddEvaluate::getDisplayOrder)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamAddEvaluate entity, Page<HubTeamAddEvaluate> page) {
        IPage<HubTeamAddEvaluate> pageList = this.page(page,
                new LambdaQueryWrapper<HubTeamAddEvaluate>()
                        .eq(StringUtils.isNotBlank(entity.getPeriodCode()), HubTeamAddEvaluate::getPeriodCode, entity.getPeriodCode())
                        .eq(StringUtils.isNotBlank(entity.getOrgCode()), HubTeamAddEvaluate::getOrgCode, entity.getOrgCode())
                        .like(StringUtils.isNotBlank(entity.getPsnName()), HubTeamAddEvaluate::getPsnName, entity.getPsnName())
                        .orderByAsc(HubTeamAddEvaluate::getDisplayOrder)
                        .orderByAsc(HubTeamAddEvaluate::getSysUpdateDate));
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamAddEvaluate entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamAddEvaluate entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamAddEvaluate> dto) {
        List<HubTeamAddEvaluate> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }
}
